package com.item.model.controller;

import com.alibaba.fastjson.JSONObject;
import com.item.model.config.KeyPairConfig;
import com.item.model.entity.result.ResultVo;
import com.item.model.util.DateUtil;
import com.item.model.util.EncryptionUtil;
import com.item.model.util.ResultVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author zhang-rongyao
 * @version V1.0
 * @Package com.item.model.controller
 * @date 2021/7/27 15:34
 */
@Slf4j
@RestController
@RequestMapping("/mq")
public class MqController {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    KeyPairConfig keyPairConfig;


    /**
     *
     * 消息队列
     *
     * 线程池注解的使用
     * 消息队列的返回
     * 怎么保证单一情况
     *
     * 加密传输
     * 解密
     *
     */

    /**
     * 加密传输，调用接口请参考测试类EEE.java 的sendInfo()
     * @param sdf
     * @return
     */
    @PostMapping("/send")
    @ResponseBody
    public ResultVo sendMsg(@RequestBody JSONObject sdf){

        try {
            String messageId = String.valueOf(UUID.randomUUID());
            String createTime = DateUtil.getFormatDateTime(new Date());
            Map<String, String> dataMap = EncryptionUtil.unCipherPassAndDate(sdf.getString("data"), sdf.getString("keyPass"), keyPairConfig.getAaaPrivateKey());
            log.info("=========解密完成============");
            String param = dataMap.get("param");
            boolean verify = EncryptionUtil.verify(param, sdf.get("sign").toString(), keyPairConfig.getZhxPublicKey());
            if(!verify) return ResultVoUtil.error("验证数据非甲方发送的");
            Map<String, Object> map = new HashMap<>();
            map.put("messageId", messageId);
            map.put("messageData", param);
            map.put("createTime", createTime);

            /**
             * 将消息携带绑定键值：topic.a 发送到交换机topicExchangeA
             * 交换机topicExchangeA只绑定了队列a,只有监听了队列a的消费者才能接收到消息
             */
            rabbitTemplate.convertAndSend("topicExchangeA", "topic.a", map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVoUtil.success();
    }

    @PostMapping("/send2")
    @ResponseBody
    public ResultVo sendMsg2(){

        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message 队列a和队列b都能接收到，队列a手动确认，队列b自动确认, hello!";
        String createTime = DateUtil.getFormatDateTime(new Date());

        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        /**
         *  将消息携带绑定键值：topic.a 发送到交换机topicExchangeA
         *  交换机topicExchangeB绑定了队列a和队列b,监听了队列a和b的消费者才能接收到消息
         */
        rabbitTemplate.convertAndSend("topicExchangeB", "topic.adsfasd", map);
        return ResultVoUtil.success();
    }
}
